﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.IO;
using System.Data;

using VIT.Provider;
using VIT.DataHelper.DataAccess;
//using Combres;
//using System.Web.Routing;

namespace VIT.Pre
{
    public class Global : System.Web.HttpApplication
    {
        //public VITContext VITData
        //{
        //    get
        //    {
        //        //if (_VITData == null)
        //        //{
        //        //    var strConn = System.Configuration.ConfigurationManager.ConnectionStrings["VITConnectionString"].ConnectionString;
        //        //    _VITData = new VITData(strConn);
        //        //}
        //        //return _VITData;
        //        if (Application["NDD_VITData"] as VITContext == null)
        //        {
        //            for (int i = 0; i < System.Configuration.ConfigurationManager.ConnectionStrings.Count; i++)
        //            {
        //                if (SelectConnectionString(System.Configuration.ConfigurationManager.ConnectionStrings[i].ConnectionString))
        //                {
        //                    var strConn = System.Configuration.ConfigurationManager.ConnectionStrings[i].ConnectionString;
        //                    Application["NDD_VITData"] = new VITContext(strConn);
        //                    break;
        //                }
        //            }
        //        }
        //        return Application["NDD_VITData"] as VITContext;
        //    }
        //}

        //private static bool SelectConnectionString(string ConnectionString)
        //{
        //    System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection(ConnectionString);
        //    try
        //    {
        //        sqlConn.Open();
        //        sqlConn.Dispose();
        //        return true;
        //    }
        //    catch
        //    {
        //        return false;
        //    }
        //}

        protected void Application_Start(object sender, EventArgs e)
        {
            //RouteTable.Routes.AddCombresRoute("Combres Route");

            Application["HomNay"] = 0;
	        Application["HomQua"] = 0;
	        Application["TuanNay"] = 0;
	        Application["TuanTruoc"] = 0;
	        Application["ThangNay"] = 0;
	        Application["ThangTruoc"] =0;
	        Application["TatCa"] = 0;
	        Application["visitors_online"] = 0;
        }

        protected void Session_Start(object sender, EventArgs e)
        {
            Session["userName"] = "";
            Session["userRoles"] = "";
            Session["language"] = "vi-VN";
            Session["category"] = "0";

            Session.Timeout = 150;
	        Application.Lock();
	        Application["visitors_online"] = Convert.ToInt32(Application["visitors_online"]) + 1;
	        Application.UnLock();
	        try
	        {
                Statistics mThongKe = new Statistics();
	            DataTable dtb = mThongKe.TableThongKe();
	            if (dtb.Rows.Count > 0)
	            {
	                Application["HomNay"] = long.Parse("0" + dtb.Rows[0]["HomNay"]).ToString("#,###");
	                Application["HomQua"] = long.Parse("0" + dtb.Rows[0]["HomQua"]).ToString("#,###");
	                Application["TuanNay"] = long.Parse("0" + dtb.Rows[0]["TuanNay"]).ToString("#,###");
	                Application["TuanTruoc"] = long.Parse("0" + dtb.Rows[0]["TuanTruoc"]).ToString("#,###");
	                Application["ThangNay"] = long.Parse("0" + dtb.Rows[0]["ThangNay"]).ToString("#,###");
	                Application["ThangTruoc"] = long.Parse("0" + dtb.Rows[0]["ThangTruoc"]).ToString("#,###");
	                Application["TatCa"] = long.Parse("0" + dtb.Rows[0]["TatCa"]).ToString("#,###");
	            }
	            dtb.Dispose();
	            mThongKe = null;
	        }
	        catch { }


            // Gio hang
            System.Data.DataTable tb = new System.Data.DataTable();
            System.Data.DataColumn col = new System.Data.DataColumn("ProductID");
            tb.Columns.Add(col);
            tb.Columns.Add("ProductName");
            tb.Columns.Add("Image");
            tb.Columns.Add("Quantity");
            tb.Columns.Add("UnitPrice");
            tb.Columns.Add("TotalCost");
            tb.Columns.Add("Language");
            System.Data.DataColumn[] keys = { col };
            tb.PrimaryKey = keys;
            //tb.Rows.Add("1", "tivi", "3", "33","44");
            Session["GioHang"] = tb;
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {
             Application.Lock();
	         Application["visitors_online"] = Convert.ToUInt32(Application["visitors_online"]) - 1;
	         Application.UnLock();
        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}